package top.lyunk.demo.gson;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;

public class 多线程TypeToken测试 {
    private static final Type TYPE = new TypeToken<List<Student>>() {
    }.getType();
    private static final Gson GSON = new Gson();

    public static void main(String[] args) {
        Student student = new Student("张三", 18, new Date(), "北京");
        Student student2 = new Student("李四", 20, new Date(), "福建");
        List<Student> studentList = List.of(student, student2);
        String json = GSON.toJson(studentList);
        System.out.println(json);


    }

    private class TypeCacheThread implements Runnable {
        private String json;
        private Type TYPE = new TypeToken<List<Student>>() {
        }.getType();

        public TypeCacheThread(String json) {
            this.json = json;
        }

        @Override
        public void run() {
            List<Student> list = GSON.fromJson(json, TYPE);
        }
    }

    private class NotTypeCacheThread implements Runnable {
        private String json;

        public NotTypeCacheThread(String json) {
            this.json = json;
        }

        @Override
        public void run() {
            List<Student> list = GSON.fromJson(json, new TypeToken<List<Student>>() {
            }.getType());
            System.out.println(list);
        }
    }


    private static class Student {
        String name;
        int age;
        Date birthday;
        String address;

        public Student() {
        }


        public Student(String name, int age, Date birthday, String address) {
            this.name = name;
            this.age = age;
            this.birthday = birthday;
            this.address = address;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public Date getBirthday() {
            return birthday;
        }

        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }
    }
}
